package com.allwees.bs.core.model.order.wf.order;

import com.baomidou.mybatisplus.core.enums.IEnum;
import com.allwees.core.wf.FsmState;
import com.allwees.core.wf.Transition;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 订单项流程状态机
 *
 * @author yangylsky
 * @since 2019-09-17
 *EOrderState
 */
public enum EOrderItemState implements FsmState<EOrderItemState, EOrderItemAction>, IEnum<String> {

	/**
	 * 新建
	 */
	START("Start", "Start", "Start", false, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Collections.singletonList(
					new Transition<>(EOrderItemAction.USER_CREATE, CREATED)
			);
		}
	},
	/**
	 * 待支付
	 */
	CREATED("Pendding Payment", "Unpaid","Unpaid",true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Arrays.asList(
					new Transition<>(EOrderItemAction.USER_CANCEL, CANCELED),
					new Transition<>(EOrderItemAction.SYSTEM_CANCEL_BY_UNPAID, CANCELED),
					new Transition<>(EOrderItemAction.USER_PAYED, WAIT_SHIP),
					new Transition<>(EOrderItemAction.USER_COD, WAIT_SHIP),
					new Transition<>(EOrderItemAction.USER_COD_CANCEL_ON_LIMIT, CANCELED)
			);
		}
	},

	/**
	 * 待物流配送
	 */
	WAIT_SHIP("Waiting For Shipment", "Processing", "Processing", true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Arrays.asList(
					new Transition<>(EOrderItemAction.USER_REQUEST_REFUND_WITH_WAIT_SHIP, WITH_WAIT_SHIP_REFUNDED),
					new Transition<>(EOrderItemAction.RETAILER_CANCEL_SHIPPED_ON_LIMIT, WITH_WAIT_SHIP_REFUNDED),
					new Transition<>(EOrderItemAction.RETAILER_CANCEL_SHIPPED_AFTER_LIMIT, WITH_WAIT_SHIP_REFUNDED),
					new Transition<>(EOrderItemAction.PLATFORM_CANCEL_SHIPPED, WITH_WAIT_SHIP_REFUNDED),
					new Transition<>(EOrderItemAction.MERCHANT_SHIPPED, SHIPPED),
					new Transition<>(EOrderItemAction.SYSTEM_CLOSE_BY_SHIPPING_OVER_TIME, CANCELED),
					new Transition<>(EOrderItemAction.USER_COD_CANCEL_ON_LIMIT, CANCELED),
					new Transition<>(EOrderItemAction.USER_COD_CANCEL_AFTER_LIMIT, CANCELED)
			);
		}
	},

	/**
	 * 配送中
	 */
	SHIPPED("Shipped", "Shipped", "Shipped", true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Arrays.asList(
					new Transition<>(EOrderItemAction.MERCHANT_SHIPPED, SHIPPED),
					new Transition<>(EOrderItemAction.LOGISTICS_DELIVERED, DELIVERED),
					new Transition<>(EOrderItemAction.SYSTEM_AUTO_RECEIVED_BY_AFTER_SHIPPING, RECEIVED),
					new Transition<>(EOrderItemAction.USER_COD_CANCEL_AFTER_LIMIT, CANCELED)
			);
		}
	},

	/**
	 * 待商家配送
	 */
	WAIT_DELIVER("Waiting For Deliver", "Waiting For Deliver", "Waiting For Deliver", true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Collections.emptyList();
		}
	},

	/**
	 * 商家已配送
	 */
	DELIVERED("Delivered", "Delivered", "Delivered", true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Collections.singletonList(
					new Transition<>(EOrderItemAction.USER_CONFIRM_RECEIVED, RECEIVED)
			);
		}
	},

	/**
	 * 用户已确认收货(待评价)
	 */
	RECEIVED("Received", "Received","Received",true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Arrays.asList(
					new Transition<>(EOrderItemAction.USER_REQUEST_REFUND_WITH_RECEIVED, WITH_RECEIVED_REFUNDING),
					new Transition<>(EOrderItemAction.USER_REVIEWED, COMPLETED)
			);
		}
	},

	/**
	 * 用户申请退货中
	 */
	WITH_SHIP_REFUNDING("Refunding", "Refunding","Refunding",true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Arrays.asList(
					new Transition<>(EOrderItemAction.PLATFORM_CONFIRM_REFUND, WITH_WAIT_SHIP_REFUNDED),
					new Transition<>(EOrderItemAction.PLATFORM_REJECT_REFUND, WAIT_SHIP)
			);
		}
	},

	/**
	 * 用户申请退货中
	 */
	WITH_RECEIVED_REFUNDING("Refunding", "Refunding","Refunding",true, false) {
		@Override
		public List<Transition<EOrderItemState, EOrderItemAction>> getTransitions() {
			return Arrays.asList(
					new Transition<>(EOrderItemAction.PLATFORM_CONFIRM_REFUND, WITH_RECEIVED_REFUNDED),
					new Transition<>(EOrderItemAction.PLATFORM_REJECT_REFUND, RECEIVED)
			);
		}
	},

	/** 已退货 */
	WITH_WAIT_SHIP_REFUNDED("Refunded", "Refunded","Refunded",false, true),

	/** 已退货 */
	WITH_RECEIVED_REFUNDED("Refunded", "Refunded","Refunded",false, true),

	/**
	 * 已退款
	 */
	WITH_WAIT_SHIP_CASH_REFUNDED("Cash Refunded", "Cash Refunded", "Cash Refunded", false, true),

	/** 已退款 */
	WITH_RECEIVED_CASH_REFUNDED("Cash Refunded", "Cash Refunded","Cash Refunded",false, true),
	CANCELED("Canceled", "Cancelled", "Cancelled", false, true),

	CLOSED("Closed", "Closed", "Closed", false, true),

	COMPLETED("Completed","Completed","Successful", false, true);

	/**
	 * 状态描述
	 */
	private final String desc;

	private final String merchantDesc;

	private final String customerDesc;

	private final boolean started;

	private final boolean ended;

	/**
	 * 数据库存储用值
	 */
	@Override
	public String getValue() {
		return name();
	}

	public String desc() {
		return desc;
	}

	@Override
	public boolean started() {
		return started;
	}

	@Override
	public boolean ended() {
		return ended;
	}

	EOrderItemState(String desc, String merchantDesc, String customerDesc, boolean started, boolean ended) {
		this.desc = desc;
		this.merchantDesc = merchantDesc;
		this.customerDesc = customerDesc;
		this.started = started;
		this.ended = ended;
	}

	public String merchantDesc() {
		return merchantDesc;
	}

	public String customerDesc() {
		return customerDesc;
	}
}
